package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.net.URI;
import java.net.URL;

import liumengbaio.lolzhangyoubao.Activity.Adapter.MoreVoiceAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreVoiceInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/14.
 */
public class MoreVoiceActivity extends AppCompatActivity {

    private MoreVoiceInfo info;
    private MoreVoiceAdapter adapter;
    private RecyclerView mRecyclerView;
    private ImageView back;
    private String url = "http://lol.zhangyoubao.com/apis/rest/AroundService/ring?page=1&i_=JfoZvarCyLfJIqs9SRFolapk" +
            "21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1473824635248&p_=18089&v" +
            "_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=Aro" +
            "undService.ring&time=1473824635&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1" +
            ".0&nonce=408558&secretSignature=BsoJVtKFjFipvlwFNc3Fu8/3PaI= ";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_voice);
        initView();
        initEvent();
        getVoiceData();
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.more_voice_recycleView);
        back = (ImageView) findViewById(R.id.more_recreation_back);
    }

    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getVoiceData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Toast.makeText(MoreVoiceActivity.this, "连接失败！", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                info = gson.fromJson(result, MoreVoiceInfo.class);
                if (info.getData().size() > 0) {
                    handler.sendEmptyMessage(1);
                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(MoreVoiceActivity.this, "数据获取失败！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    adapter = new MoreVoiceAdapter(MoreVoiceActivity.this, info);
                    mRecyclerView.setAdapter(adapter);
                    LinearLayoutManager llm = new LinearLayoutManager(MoreVoiceActivity.this);
                    mRecyclerView.setLayoutManager(llm);
                    break;
            }
        }
    };
}
